package com.lex.cheeseStrip.ui;

import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.dh.cheesestrip.HttpUtil.HttpMode;
import com.dh.cheesestrip.HttpUtil.HttpSender;
import com.dh.cheesestrip.HttpUtil.callback.StringCallback;
import com.dh.cheesestrip.Util.LogUtil;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.lex.cheeseStrip.R;
import com.lex.cheeseStrip.adapter.AdapterDemo;
import com.lex.cheeseStrip.app.BaseAct;
import com.lex.cheeseStrip.bean.push.ListPush;

import java.io.UnsupportedEncodingException;

import butterknife.BindView;

public class DemoAct extends BaseAct implements AdapterView.OnItemClickListener {

    @BindView(R.id.lv_demo)
    ListView lvDemo;
    @BindView(R.id.tv_demo)
    TextView tvDemo;

    long start,over;

    private String[] str = {"HTTP请求"};

    private AdapterDemo adapterDemo;

    private RuntimeSchema<ListPush> schema = RuntimeSchema.createFrom(ListPush.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        init(this);

        adapterDemo = new AdapterDemo(getActivity(), str);
        lvDemo.setAdapter(adapterDemo);
        lvDemo.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (i) {
            default:
                break;
            case 0:
                HttpSender.getInstance().setHttpMode(HttpMode.FREE).setUrl("http://192.168.22.193/protostuff").setContext(getActivity()).setCallback(new StringCallback() {
                    @Override
                    public void onResponse(String response) {
                        over = System.currentTimeMillis();
                        ListPush list = schema.newMessage();
                        try {
                            ProtostuffIOUtil.mergeFrom(response.getBytes("utf-8"),list,schema);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtil.i(list.toString());
                        tvDemo.setText("请求时间:"+(over-start)+" ms");
                    }


                }).sender();
                start = System.currentTimeMillis();
                break;
        }
    }
}
